package cn.pxs.controller;

import cn.pxs.entity.Cart;
import cn.pxs.entity.OrdItem;
import cn.pxs.entity.Order;
import cn.pxs.service.CartService;
import cn.pxs.service.OrdService;
import cn.pxs.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/fore/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    OrdService ordService;

    @ResponseBody
    @RequestMapping("/findAll")
    public Map findAll(){
        Object user =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return RespJson.pack(200,"你自己的购物车",cartService.findAll(user));
    }

    @ResponseBody
    @RequestMapping("/addCart2Order")
    //上传参数 JSON格式 List
    public Map addCart2Order(@RequestBody List<OrdItem> ordItems){
        Object user =
                SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(user == null)  return RespJson.pack(401,"未登录",null);
        Order o = new Order();
        o.setIspay(0);
        Order order = cartService.addCart2Order(o, ordItems);
        if(order==null){
            return RespJson.pack(500,"查询数据失误..",null);
        }
        return RespJson.pack(200,"成功，产生订单",order);
    }


}
